package oop.data;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oop.db.dao.AnswerDAO;
import oop.db.dao.SectionDAO;

public class Question extends BaseQuestion {

	private long sectionId;
	private List<Answer> usedAnswers = null;
	private Map<Long, Answer> usedAnswerMap = null;
	private int mark;

	public Question(long id, long contentId, int level, long topicId,
			long authorId, long sectionId, Date createDate) {
		super(id, contentId, level, topicId, authorId, createDate);
		this.sectionId = sectionId;
	}

	public Map<Long, Answer> getUsedAnswerById() throws SQLException {
		if (usedAnswerMap == null) {
			usedAnswerMap = new HashMap<Long, Answer>();
			for (Answer answer : getUsedAnswers()) {
				usedAnswerMap.put(answer.getId(), answer);
			}
		}
		return usedAnswerMap;
	}
	
	public List<Answer> getUsedAnswers() throws SQLException {
		if (usedAnswers == null) {
			usedAnswers = AnswerDAO.fetchByQuestion(this);
		}
		return usedAnswers;
	}
	
	public int getMark() {
		return mark;
	}
	
	public void setMark(int mark) {
		this.mark = mark;
	}

	public Section getSection() throws SQLException {
		return SectionDAO.fetchById(sectionId).get(0);
	}

	void setSection(Section section) {
		this.sectionId = section.getId();
	}

}
